import Vue from 'vue';
import Vuex from 'vuex';
import storageH5 from './storageH5.js';

Vue.use(Vuex)

// 计算总价 price (不含配送，包装费) 和 产品总包材费用 boxFee
function countTotalPrice(cartList) {
	let dicountPrice = 0; // 折扣总价
	let price = 0; // 产品总价
	let boxFee = 0; // 餐盒总费用
	let boxNum = 0; // 餐盒数量
	if (cartList && cartList.length > 0) {
		cartList.map(item => {

			if (item.couponId) {
				dicountPrice += Number((item.productCount * (item.orinPrice - (item.addPrice || 0) - item.specialPrice || 0)).toFixed(2));
				price += Number((item.productCount * (item.specialPrice + (item.addPrice || 0))).toFixed(2));
			} else {
				dicountPrice += Number((item.productCount * item.freePrice).toFixed(2));
				price += Number((item.productCount * item.salePrice).toFixed(2));
			}
			
			boxFee += Number((item.productCount * item.boxFee).toFixed(2));
			boxNum += item.boxNum;
		});
		
		dicountPrice = Number(dicountPrice.toFixed(2));
		price = Number(price.toFixed(2));
	}
	
	return {
		dicountPrice,
		price,
		boxFee,
		boxNum
	};
}

// 订单数据
let orderData = {
	cartProductFeeTotal:0,// 购物车产品总金额
	cartProductDiscount:0,// 购物车里产品优惠金额
	cartBoxFeeTotal:0,// 购物车里餐盒费金额
	cartBoxNum:0, // 购物车里餐盒数量
	couponDiscount:0,// 券优惠金额
	orderTotal: 0,// 订单总金额
	accountPay: 0,// 订单应付金额
	discountTotal: 0,// 订单总优惠=产品优惠+券优惠
	boxFee: 0,// 餐盒费
	shippingFee: 0,// 配送费
	diningTime:'',// 就餐时间
	remark: ''// 备注
};

// 取session缓存数据
let diningMode = uni.getStorageSync('diningMode') || 'wx_zq'; // 就餐方式
let targetPoint = storageH5.get('targetPoint') ? JSON.parse(storageH5.get('targetPoint')) : {}; // 目标定位点
let exclusionMap = storageH5.get('exclusionMap') ? JSON.parse(storageH5.get('exclusionMap')) : {}; // 互斥映射库
let relevanceMap = storageH5.get('relevanceMap') ? JSON.parse(storageH5.get('relevanceMap')) : {}; // 关联映射库
let productsMap = storageH5.get('productsMap') ? JSON.parse(storageH5.get('productsMap')) : {}; // 产品映射库
let currentStoreInfo = storageH5.get('currentStoreInfo') ? JSON.parse(storageH5.get('currentStoreInfo')) : {}; // 当前门店信息
let deliveryAddress = storageH5.get('deliveryAddress') ? JSON.parse(storageH5.get('deliveryAddress')) : {}; // 配送地址
let cartList = storageH5.get('cartList') ? JSON.parse(storageH5.get('cartList')) : []; // 购物车列表
let order = storageH5.get('order') ? JSON.parse(storageH5.get('order')) : orderData; // 订单数据

const store = new Vuex.Store({ 
	state: {
		diningMode,
		targetPoint,
		allStoreList: [], // 所有门店
		exclusionMap,
		relevanceMap,
		productsMap,
		currentStoreInfo,
		deliveryAddress,
		cartList,
		order,
		cartTotalNum: 0, // 购物车数量
		// 单品产品信息
		singleProductInfo: {
			product: {}, // 产品信息
			fromPage: 'menu' // 来源页面
		},
		isShowSinglePop: false, // 是否显示选单品弹窗
	},
	mutations: {
		// 处理就餐方式
		handleDiningMode(state, val) {
			state.diningMode = val;
			uni.setStorageSync('diningMode', val);
		},
		
		// 处理目标定位点
		handleTargetPoint(state, val) {
			state.targetPoint = val;
			storageH5.set('targetPoint', JSON.stringify(val));
		},
		
		// 所有门店列表
		cacheAllStoreList(state, val) {
			state.allStoreList = val;
		},
		
		// 缓存当前门店信息
		cacheCurrentStoreInfo(state, val) {
			const lastStore = state.currentStoreInfo; // 上一家门店
			const cartList = state.cartList;
			
			// 切换门店
			if (lastStore.shopID && lastStore.shopID !== val.shopID && cartList.length) {
				uni.setStorageSync('EEM', 'yes'); // 执行排除方法标识
			}
		
			state.currentStoreInfo = val;
			storageH5.set('currentStoreInfo', JSON.stringify(val));
		},
		
		// 缓存产品互斥映射库
		cacheExclusionMap(state, val) {
			state.exclusionMap = val;
			storageH5.set('exclusionMap', JSON.stringify(val));
		},
		
		// 缓存产品关联映射库
		cacheRelevanceMap(state, val) {
			state.relevanceMap = val;
			storageH5.set('relevanceMap', JSON.stringify(val));
		},
		
		// 缓存产品映射库
		cacheProductsMap(state, val) {
			state.productsMap = val;
			storageH5.set('productsMap', JSON.stringify(val));
		},
		
		// 处理购物车 加减商品，
		handleCartList(state, val) {
			state.cartList = val;
			
			state.cartTotalNum = val.reduce((acc, item) => {
				return acc + item.productCount;
			}, 0);
			
			const priceObj = countTotalPrice(val);
		
			let order = state.order;
			order.cartProductFeeTotal = priceObj.price; // 购物车产品总金额
			order.cartProductDiscount = priceObj.dicountPrice; // 购物车里产品优惠金额
			order.cartBoxFeeTotal = priceObj.boxFee; // 购物车里餐盒费金额
			order.cartBoxNum = priceObj.boxNum; // 购物车里餐盒数量	
			state.order = order;
			
			storageH5.set('cartList', JSON.stringify(val));
			storageH5.set('order', JSON.stringify(order));
		},
		
		// 清空购物车
		clearCartList(state, val) {
			if (val) {
				state.cartList = [];
				state.cartTotalNum = 0;
		
				let order = state.order;
				order.cartProductFeeTotal = 0; // 购物车产品总金额
				order.cartProductDiscount = 0; // 购物车里产品优惠金额
				order.cartBoxFeeTotal = 0; // 购物车里餐盒费金额
				order.cartBoxNum = 0; // 购物车里餐盒数量	
				state.order = order;
		
				storageH5.set('cartList', JSON.stringify([]));
				
				this.commit("clearOrderData", orderData);
			}
		},
		
		// 处理订单数据
		handleOrderData(state, val) {
			state.order = val;
			storageH5.set('order', JSON.stringify(val));
		},
		
		// 清空订单数据
		clearOrderData(state, val) {
			state.order = orderData;
			storageH5.set('order', JSON.stringify(orderData));
		},
		
		// 处理单品产品信息
		handleSingleProductInfo(state, val) {
			state.singleProductInfo = { 
				...val
			};
		},
		
		// 处理显示单品弹窗事件
		handleShowSinglePop(state, val) {
			state.isShowSinglePop = val;
		},
		
	},
	actions: {

	}
})
export default store
